﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using ScrumFactory.Composition.ViewModel;
using ScrumFactory.Composition;
using ScrumFactory.Windows.Helpers;
using ScrumFactory.Services;
using System.Linq;
using ScrumFactory.Composition.View;
using System.Windows.Data;
using System.Windows.Input;

namespace ScrumFactory.Team.ViewModel {
    
    [Export]
    [Export(typeof(IProjectTabViewModel))]    
    public class MyDayViewModel : MemberDetailBase, IProjectTabViewModel, INotifyPropertyChanged {

        private ICollection<ProjectEvent> projectEvents;

        [ImportingConstructor]
        public MyDayViewModel(
            [Import] IEventAggregator aggregator,
            [Import] ITasksService tasksService,
            [Import] IProjectsService projectsService,
            [Import] ITeamService teamService,
            [Import] IBackgroundExecutor executor,
            [Import] IAuthorizationService authorizator) : base(aggregator, tasksService, projectsService, teamService, executor, authorizator)  {

            aggregator.Subscribe<MemberProfile>(ScrumFactoryEvent.SignedMemberChanged, OnSignedMemberChanged);

            OnLoadCommand = new DelegateCommand(OnLoad);
            ShowDetailWindowCommand = new DelegateCommand<MemberViewModel>(ShowMemberDetail);
            
        }

        [Import]
        public IPendingMembershipsListViewModel PendingMembershipsListViewModel { get; set; }

        [Import]
        private MemberDetailViewModel MemberDetailViewModel  { get; set; }

        private void ShowMemberDetail(MemberViewModel member) {
            MemberDetailViewModel.Show(member.MemberProfile);
        }

        private void OnSignedMemberChanged(MemberProfile member) {
            NeedRefresh = true;
            MemberProfile = member;
            OnLoad();
        }

        private void OnLoad() {
            IsVisible = (authorizator != null && authorizator.SignedMemberProfile != null);            
            if(NeedRefresh && IsVisible) Load();
        }

        private System.DateTime lastRefreshDate;

        public override bool NeedRefresh {
            get {
                return System.DateTime.Now.Subtract(lastRefreshDate).TotalSeconds > 10;
            }
            set {
                if (value == false)
                    lastRefreshDate = System.DateTime.Now;
                base.NeedRefresh = value;
            }
        }

        public ICollection<ProjectEvent> ProjectEvents {
            get {
                return projectEvents;
            }
            set {            
                projectEvents = value;                
                OnPropertyChanged("ProjectEvents");
            }
        }

      

      

        private void Load() {


            LoadMemberTasks();
            LoadMemberOwnedTasks();
            LoadMemberEngagedProjects();
            LoadMemberWorkingWithMembers();
            LoadEvents();

            NeedRefresh = false;
        }

        

        private void LoadEvents() {
            IsLoadingData = true;

            executor.StartBackgroundTask<ICollection<ProjectEvent>>(
                () => { return projectsService.GetUserProjectEvents(); },
                events => {
                    ProjectEvents = new ObservableCollection<ProjectEvent>(events.OrderBy(t => t.When));
                    
                    IsLoadingData = false;
                });
        }


        public string PanelName {
            get { return Properties.Resources.My_day; }
        }

        public int PanelDisplayOrder {
            get { return 0; }
        }

        private bool isVisible;
        public bool IsVisible {
            get {
                return isVisible;
            }
            set {
                isVisible = value;
                OnPropertyChanged("IsVisible");
            }
        }

        public bool IsEspecialTab {
            get {
                return true;
            }
        }
      

        [Import(typeof(MyDay))]
        public IView View { get; set; }


        public ICommand OnLoadCommand { get; set; }
        public ICommand ShowDetailWindowCommand { get; set; }

    }
}
